package com.purchase.util;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.StringConverter;

public final class MyBeanUtils {
	private MyBeanUtils() {
	}

	public static void populate(Object bean, Map<String, Object> properties) {
		 try {
		        //处理时间格式
		        DateConverter dateConverter = new DateConverter();
		        //设置日期格式
		        dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm"});
		        //注册格式
		        ConvertUtils.register(dateConverter, Date.class);
		        // 注册String->decimal
				//StringConverter stringConverter = new StringConverter(null);
				//ConvertUtils.register(stringConverter, BigDecimal.class);
		        //封装数据
		        BeanUtils.populate(bean, properties);
		    } catch (IllegalAccessException | InvocationTargetException e) {
		        throw new RuntimeException(e);
		    }
	}
}
